from fastapi import APIRouter
from typing import List
from apps.models import Course,Course_Copy
from apps.schemas.course import StudentCourseResponse,StudentCourseCreate,Student_myCourseResponse,Student_CourseCreate

student = APIRouter()

# 学生查看所有课程
@student.get("/stu_course/{status}",response_model=List[StudentCourseResponse])
async def get_stu_course(status: str):
    """
    获取所有课程
    :return:
    """
    courses = await Course.filter(status=status).prefetch_related("user_id")
    return courses
# 学生查看自己是否已拥有该课程
@student.get("/stu_course/{extral_1}/{user_id}")
async def get_is_stu_course(extral_1: str,user_id: int):
    """
    查看自己是否已拥有该课程
    :param extral_1:
    :param user_id:
    :return:
    """
    course = await Course.filter(extral_1=extral_1, user_id_id=user_id).first()
    if course:
        return {"code": 200, "msg": "已拥有该课程"}
    else:
        return {"code": 400, "msg": "未拥有该课程"}
# 学生添加自己的课程
@student.post("/add_stu_course")
async def add_stu_course(body: StudentCourseCreate):
    """
        添加自己的课程
    """
    course = await Course.create(**body.dict())
    return {"code": 200, "msg": "添加成功", "data": course}
@student.post("/add_stu_course_copy")
async def add_stu_course_copy(body: Student_CourseCreate):
    """
        添加自己的课程
    """
    course = await Course_Copy.create(**body.dict())
    return {"code": 200, "msg": "添加成功", "data": course}
# 学生查看自己的课程
@student.get("/my_stu_course/{id}",response_model=List[Student_myCourseResponse])
async def get_my_stu_course(id: int):
    """
    获取自己的所有课程
    """
    courses = await Course.filter(user_id_id=id).all()
    cla_list = []
    for course in courses:
        cls = await Course.filter(extral_2 = course.extral_2, user_id__login_id__role="teacher" ).prefetch_related("user_id__login_id").all()
        for cla_ in cls:
            cla_list.append(cla_)
    return cla_list
